package com.zyb.patient.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.zyb.patient.R;
import com.zyb.patient.adapter.AboutMeSelectCityAdapter;
import com.zyb.patient.dto.CityDto;
import com.zyb.patient.dto.UserDto;

/**
 * update userCity type 地区
 * 
 * @author luoming
 */
public class UserForUpdateCityActivity extends BaseActivity implements
		OnClickListener {

	private ListView cityListView;
	private List<CityDto> allCtiyData, currentCityData;
	private AboutMeSelectCityAdapter cityAdapter;
	private UserDto userInfo;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_usercenter_update_city);

		initData();

		initView();

		overridePendingTransition(R.anim.enter_righttoleft, R.anim.noanim);
	}

	private void initData() {
		userInfo = getcurrentUser();
		allCtiyData = db.selectCityDtoList(getIntent().getStringExtra(
				"parentId"));
		currentCityData = new ArrayList<CityDto>();
		for (CityDto dto : allCtiyData) {
			if (dto.region_id.equals(userInfo.province)
					|| dto.region_id.equals(userInfo.city)
					|| dto.region_id.equals(userInfo.area)) {
				dto.flag = "1";
			}
			if (db.isHasMoreCityById(dto.region_id)) {
				dto.hasMore = "1";
			}
			currentCityData.add(dto);
		}

	}

	private void initView() {
		((TextView) findViewById(R.id.head_layout_tv)).setText("地区");
		findViewById(R.id.head_layout_back).setOnClickListener(this);

		cityListView = ((ListView) findViewById(R.id.listView));
		cityAdapter = new AboutMeSelectCityAdapter(mContext, currentCityData);
		cityListView.setAdapter(cityAdapter);
		cityListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				if ("1".equals(currentCityData.get(pos).hasMore)) {
					Intent it = new Intent(mContext,
							UserForUpdateCityActivity.class);
					it.putExtra("parentId", currentCityData.get(pos).region_id);
					startActivityForResult(it, RESULT_FIRST_USER);
				} else {
					// 选中城市
					Intent it = new Intent(
							AboutMeUserCenterActivity.COM_CHANGE_USER_CITY);
					it.putExtra("area", currentCityData.get(pos).region_id);
					UserForUpdateCityActivity.this.sendBroadcast(it);
					setResult(RESULT_OK);
					UserForUpdateCityActivity.this.finish();
				}

			}
		});
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.head_layout_back) {
			super.onBackPressed();
		}
	}

	@Override
	public void refresh(Object... param) {
		try {
			int operType = (Integer) param[0];
			Object obj = param[1];
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			switch (operType) {
			// case Task.SEND_SAVE_PHONE:
			// if (obj != null) {
			// UserDto dto = (UserDto) obj;
			// if ("1".equals(dto.result)) {
			// showToast("预留电话成功，请等待客户人员联系您");
			// finish();
			// } else
			// showToast(dto.message);
			//
			// } else {
			// showToast(R.string.http_no_net_tip);
			// }
			// break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case RESULT_FIRST_USER:
			setResult(RESULT_OK);
			finish();
			break;

		default:
			break;
		}

	}

}